﻿using Jdwl.Api.Message;
using Jdwl.Api.Message.Protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 测试WebSocket
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        private static JdMessageClient client;
        private static readonly object locker = new object();
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (client == null)
                {
                    lock (locker)
                    {
                        if (client == null)
                        {

                                string  groupName = groupnametext.Text;
                                string appKey = keytext.Text;
                                string appSecret = secrettext.Text;
                                string wss_url = urltext.Text;
                                client = new JdMessageClient(appKey, appSecret, groupName);
                                client.OnMessage += OnMessageHandler;
                                client.Connect(wss_url);

                        }
                    }
                }
            }

            catch (Exception ex)
            {
                PrintStackTrace(ex);
            }
        }
        private void OnMessageHandler(object sender, JdEventArgs e)
        {
            try
            {
               
            }
            catch (Exception ex)
            {
                PrintStackTrace(ex);
                e.Fail();
            }
        }

        

        internal static void PrintStackTrace(Exception e)
        {
            Console.WriteLine($"Exception Type {e.GetType().Name}, Message:{e.Message},Stack:{e.StackTrace}");
            if (e.InnerException != null)
            {
                Console.Write(" inner Exception: ");
                PrintStackTrace(e.InnerException);
            }
        }
        private void Secrettext_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (client != null)
            {
                client.Dispose();
            }
        }
    }
}
